package net.wangds.utils;

import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;

public class ImageUtils {

    public static BufferedImage toImage(byte[] img) {
        try (ByteArrayInputStream is = new ByteArrayInputStream(img)) {
            return ImageIO.read(is);
        } catch (IOException e) {
            LoggerFactory.getLogger(ImageUtils.class).info(e.getLocalizedMessage(), e);
        }
        return null;
    }

    /**
     * 图片二进制数据转换为html支持的src属性地址.
     * @param data 图片.
     * @param format 格式.
     *               <ul>支持的格式:<li>image/jpeg</li>
     *               <li>image/png</li>
     *               <li>image/gif</li></ul>
     * @return 结果.
     */
    public static String imgBytes2Blob(byte[] data, String format){
        //data:image/jpeg;base64,
        StringBuffer sb = new StringBuffer();
        sb.append("data:").append(format).append(";base64,").append(Base64.getEncoder().encodeToString(data));
        return sb.toString();
    }

    /**
     * 图片二进制数据转换为html支持的src属性地址.
     * @param data 图片.
     * @param format 格式.
     * @return 结果.
     */
    public static String imgBytes2Blob(byte[] data, ImageFormatEnum format){
        //data:image/jpeg;base64,
        StringBuffer sb = new StringBuffer();
        String str;
        switch(format){
            case JPEG:
                str = "image/jpeg";
                break;
            case GIF:
                str = "image/gif";
                break;
            case PNG:
            default:
                str = "image/png";
        }
        sb.append("data:").append(str).append(";base64,").append(Base64.getEncoder().encodeToString(data));
        return sb.toString();
    }

    /**
     * 获得图片指定格式数据.
     * @param img 图片.
     * @param format 格式.
     *                <p>根据JDK支持情况确定,目前至少支持:<ul><li>bmp</li><li>jpg</li><li>png</li></ul></p>
     * @return 结果.
     */
    public static byte[] toBytes(BufferedImage img, String format) {
        try (ByteArrayOutputStream buf = new ByteArrayOutputStream()) {
            ImageIO.write(img, format, buf);
            buf.flush();
            return buf.toByteArray();
        } catch (IOException e) {
            LoggerFactory.getLogger(ImageUtils.class).info(e.getLocalizedMessage(), e);
        }
        return null;
    }

    public static byte[] scaleImage(byte[] image, int left, int top, int width, int height, String format) {
        byte[] res = new byte[0];
        BufferedImage img = toImage(image);
        if (img == null) {
            return res;
        }

        BufferedImage out = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = out.getGraphics();
        g.drawImage(img, 0, 0, width, height, left, top, width, height, null);
        g.dispose();
        return toBytes(out, format);

    }

    public static byte[] scaleImage(byte[] image, int targetSize, String format) {
        byte[] res = new byte[0];
        BufferedImage img = toImage(image);
        if (img == null) {
            return res;
        }
        float originSize = image.length;

        float originWidth = img.getWidth();
        float originHight = img.getHeight();

        float scale = targetSize / originSize;

        int targetWidth = (int) Math.ceil(originWidth * scale);
        int targetHeight = (int) Math.ceil(originHight * scale);

        Image targetImg = img.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH);
        BufferedImage out = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
        Graphics g = out.getGraphics();
        g.drawImage(targetImg, 0, 0, null);
        g.dispose();

        return toBytes(out, format);
    }

}
